#ifndef __CONDITION_H__
#define __CONDITION_H__

#include <pthread.h>
#include "mutex_lock.h"

class Condition
{
 public:
  Condition(MutexLock& mutex) : mutex_(mutex)
  { pthread_cond_init(&pcond_, NULL); }

  ~Condition()
  { pthread_cond_destroy(&pcond_); }

  void wait()
  { pthread_cond_wait(&pcond_, mutex_.getPthreadMutex()); }

  void notify()
  { pthread_cond_signal(&pcond_); }

  void notifyAll()
  { pthread_cond_broadcast(&pcond_); }

 private:
  MutexLock& mutex_;
  pthread_cond_t pcond_;
};

class CountDownLatch
{
 public:
  CountDownLatch(int count)
   : count_(count),
     mutex_(),
     condition_(mutex_)
  { }

 private:
  int count_;
  MutexLock mutex_;  // 顺序很重要
  Condition condition_;
};

#endif